package cn.com.oceansoft.business.mobile.danger.util;


import cn.com.oceansoft.business.danger.entity.Ptcsbz;
import cn.com.oceansoft.nos.cache.redis.CacheBaseService;
import cn.com.oceansoft.nos.common.util.HttpClientUtil;
import org.apache.commons.io.output.ByteArrayOutputStream;
import sun.misc.BASE64Encoder;
import sun.net.www.protocol.http.HttpURLConnection;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhuya
 */
public class PicBzThread extends Thread  {
    //final private static String pvUrl = "http://192.168.1.2:8089/fastdfs/api/fastdfs/upload";
    final private static String pvUrl = "http://172.16.3.6/jdcj_fastdfs_client/api/fastdfs/upload";
    Map<String, String> paraMap = new HashMap<String, String>();
    private Ptcsbz ptcsbz;
    private String fileExt;
    private String[] arr;
    public PicBzThread() {

    }

    public PicBzThread(Ptcsbz ptcsbz) {
        this.ptcsbz = ptcsbz;
    }

    public void run() {
        System.out.println("PicFkThread.start()");
        paraMap.put("orderdbid",ptcsbz.getId());
        //paraMap.put("type",dangerContent.getStatus());
        for(int l=1;l<=9;l++){
            arr=null;
            if(l==1&&""!=ptcsbz.getDealphoto1()&&null!=ptcsbz.getDealphoto1()){
                arr=ptcsbz.getDealphoto1().split(",");
                paraMap.put("type","16"); ;//电动车停放点建成数量（个） //灭火器（具）
            }
            if(l==2&&""!=ptcsbz.getDealphoto2()&&null!=ptcsbz.getDealphoto2()){
                arr=ptcsbz.getDealphoto2().split(",");
                paraMap.put("type","17"); //电动车停放点面积（M²） //
            }
            if(l==3&&""!=ptcsbz.getDealphoto3()&&null!=ptcsbz.getDealphoto3()){
                arr=ptcsbz.getDealphoto3().split(",");
                paraMap.put("type","18");  //可停放电动车（辆）  //
            }
            if(l==4&&""!=ptcsbz.getDealphoto4()&&null!=ptcsbz.getDealphoto3()){
                arr=ptcsbz.getDealphoto4().split(",");
                paraMap.put("type","19");//新增灭火器（具）
            }
            if(l==5&&""!=ptcsbz.getDealphoto5()&&null!=ptcsbz.getDealphoto5()){
                arr=ptcsbz.getDealphoto5().split(",");
                paraMap.put("type","20");//应急照明灯（具）
            }
            if(l==6&&""!=ptcsbz.getDealphoto6()&&null!=ptcsbz.getDealphoto6()){
                arr=ptcsbz.getDealphoto6().split(",");
                paraMap.put("type","21");//简易喷淋（套）
            }
            if(l==7&&""!=ptcsbz.getDealphoto7()&&null!=ptcsbz.getDealphoto7()){
                arr=ptcsbz.getDealphoto7().split(",");
                paraMap.put("type","13");//新增独立式烟感报警器（只）
            }
            if(l==8&&""!=ptcsbz.getDealphoto8()&&null!=ptcsbz.getDealphoto8()){
                arr=ptcsbz.getDealphoto8().split(",");
                paraMap.put("type","14");//新增电器火灾监控系统（套）
            }
            if(l==9&&""!=ptcsbz.getDealphoto9()&&null!=ptcsbz.getDealphoto9()){
                arr=ptcsbz.getDealphoto9().split(",");
                paraMap.put("type","15");//新增可燃气体紧急切断装置（套）
            }
            if(arr!=null){
                for(int i=0;i<arr.length;i++){
                    if(arr[i]!=null&&!"".equals(arr[i])){
                        try{
                            paraMap.put("base64",getBase64(arr[i]));
                            paraMap.put("extname",fileExt);
                            String a = HttpClientUtil.doPost(pvUrl, paraMap, "utf-8");
                            //System.out.println(a);
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }

        }
        System.out.println("PicFkThread.end()");
    }
    private String getBase64(String serverId) throws IOException {
        InputStream is = null;
        Long m_id = new Long(String.valueOf("1056"));
        String accessToken = "";
        String cachekey = "CMCX_ACCESSTOKEN";
        accessToken = CacheBaseService.getInstance().get(cachekey);
        //accessToken ="10_rAjZmBjBf1OPb7lLoerkhGRHoidfGToe2XsNryK2eEfTBKy8qTAE40VoY0BUkD9jLr16qzfkA7Syq5_NvDBB5UoLTiqmlDTCN6QSmhSZ95XdJNrO-u936y0cPd2VsVlsKd6-WDTD_b2m8y9kPGCfAJAEVR";
        // 拼接请求地址
        String requestUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("MEDIA_ID",serverId);
        URL url = new URL(requestUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setRequestMethod("GET");
        is = conn.getInputStream();
        byte[] output = input2byte(is); //in_b为转换之后的结果
        // 根据内容类型获取扩展名
        String fileExt = getFileexpandedName(conn.getHeaderField("Content-Type"));
        this.fileExt=fileExt;
        BASE64Encoder encoder = new BASE64Encoder();
        String outstr = encoder.encode(output);
       // System.out.println("base64长度"+output.length);
        return outstr;
    }
    private String getFileexpandedName(String contentType) {
        String fileEndWitsh = "";
        if ("image/jpeg".equals(contentType))
            fileEndWitsh = "jpg";
        else if ("audio/mpeg".equals(contentType))
            fileEndWitsh = "mp3";
        else if ("audio/amr".equals(contentType))
            fileEndWitsh = "amr";
        else if ("video/mp4".equals(contentType))
            fileEndWitsh = "mp4";
        else if ("video/mpeg4".equals(contentType))
            fileEndWitsh = "mp4";
        else
            fileEndWitsh = "jpg";
        return fileEndWitsh;
    }
    private byte[] input2byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }
}
